<?php 

declare(strict_types=1);

namespace WanZ\App\Utils\Prometheus;

use WanZ\App\User\Contracts\UserRequestType;
use Prometheus\Exception\MetricsRegistrationException;

/**
 * Class AgentBusinessMan
 * @package WanZ\App\Utils\Prometheus
 *
 */
class AgentBusiness extends Prometheus
{
    /**
     * 记录代理商注册失败次数
     *
     * @param string $phone
     * @param int $isSupervise
     * @param int $errorNum
     */
    public function addErrorNumToAgentRegister(string $phone, int $isSupervise = 0, int $errorNum = 1)
    {
        return;

        $labels = [
            'agent_phone' => $phone,
            'is_supervise' => $isSupervise
        ];

        $labels = array_merge($this->getDefaultLabel(), $labels);
        $this->incBy('register', $this->getDefaultName(), '代理商注册', $labels, $errorNum);
    }

    /**
     * 记录获取代理商信息错误次数
     *
     * @param int $agentId
     * @param int $errorNum
     * @throws MetricsRegistrationException
     */
    public function addErrorNumToGetAgentInfo(int $agentId, int $errorNum = 1)
    {
        return;

        $labels = [
            'agent_id' => $agentId
        ];

        $labels = array_merge($this->getDefaultLabel(), $labels);
        $this->incBy('getInfo', $this->getDefaultName(), '获取代理商信息', $labels, $errorNum);
    }

    /**
     * 记录代理商进件相关错误次数
     *
     * @param string $resource 操作源头 内部添加/外部添加
     * @param string $superviseType 监管平台
     * @param int $errorNum 错误次数
     * @param string $errorMsg 错误信息
     */
    public function addErrorNumToAgentSupervise(
        string $resource,
        string $superviseType = 'adaPay',
        int $errorNum = 1,
        string $errorMsg = ''
    ) {
        $labels = ['resource' => $resource];
        if (!empty($errorMsg)) {
            $labels['errorMsg'] = $errorMsg;
        }

        $helpMsg = '未知平台';
        switch ($superviseType) {
            case 'adaPay':
                $helpMsg = '汇付天下平台';
                break;
            default:
                break;
        }

        $labels = array_merge($this->getDefaultLabel(), $labels);
        $this->incBy('supervise', $this->getDefaultName(), $helpMsg, $labels, $errorNum);
    }

    /**
     * 记录运营商添加维护人失败错误次数
     *
     * @param int $operatorId
     * @param string $maintenancePhone
     * @param int $errorNum
     */
    public function addErrorNumToMaintenance(int $operatorId, string $maintenancePhone, int $errorNum = 1)
    {
        return;

        $labels = [
            'operator_id' => $operatorId,
            'maint_phone' => $maintenancePhone
        ];

        $labels = array_merge($this->getDefaultLabel(), $labels);
        $this->incBy('add', 'maintenance_error_total', '运营商添加维护人员', $labels, $errorNum);
    }

    /**
     * 获取默认标签
     * @return array
     */
    private function getDefaultLabel(): array
    {
       return $this->getCompanyIdAndUType();
    }

    /**
     * 获取默认指标的后缀名
     * @return string
     */
    private function getDefaultName(): string
    {
        $data = $this->getCompanyIdAndUType();
        $obj = make(UserRequestType::class);

        switch ($data['u_type']) {
            case $obj->getShareUserType():
                $name = 'share_error_total';
                break;
            case $obj->getMaintenanceUserType():
                $name = 'maintenance_error_total';
                break;
            case $obj->getAdminUserType():
                $name = 'admin_error_total';
                break;
            case $obj->getOperatorUserType():
            default:
                $name = 'operator_error_total';
                break;
        }

        $obj = null;
        return $name;
    }
}
